package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.app.domain.bo.AppUserCardBo;
import com.ruoyi.app.domain.vo.AppUserCardVo;
import com.ruoyi.app.domain.AppUserCard;
import com.ruoyi.app.mapper.AppUserCardMapper;
import com.ruoyi.app.service.IAppUserCardService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 卡片管理Service业务层处理
 *
 * @author Zach
 * @date 2023-11-14
 */
@RequiredArgsConstructor
@Service
public class AppUserCardServiceImpl implements IAppUserCardService {

    private final AppUserCardMapper baseMapper;

    /**
     * 查询卡片管理
     */
    @Override
    public AppUserCardVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询卡片管理列表
     */
    @Override
    public TableDataInfo<AppUserCardVo> queryPageList(AppUserCardBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppUserCard> lqw = buildQueryWrapper(bo);
        Page<AppUserCardVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询卡片管理列表
     */
    @Override
    public List<AppUserCardVo> queryList(AppUserCardBo bo) {
        LambdaQueryWrapper<AppUserCard> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppUserCard> buildQueryWrapper(AppUserCardBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppUserCard> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCardNo()), AppUserCard::getCardNo, bo.getCardNo());
        lqw.eq(bo.getCardType() != null, AppUserCard::getCardType, bo.getCardType());
        lqw.eq(bo.getCardBank() != null, AppUserCard::getCardBank, bo.getCardBank());
        lqw.eq(bo.getCardDate() != null, AppUserCard::getCardDate, bo.getCardDate());
        lqw.eq(StringUtils.isNotBlank(bo.getCardEndorse()), AppUserCard::getCardEndorse, bo.getCardEndorse());
        return lqw;
    }

    /**
     * 新增卡片管理
     */
    @Override
    public Boolean insertByBo(AppUserCardBo bo) {
        AppUserCard add = BeanUtil.toBean(bo, AppUserCard.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改卡片管理
     */
    @Override
    public Boolean updateByBo(AppUserCardBo bo) {
        AppUserCard update = BeanUtil.toBean(bo, AppUserCard.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppUserCard entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除卡片管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据卡号查询
     * @param cardNo
     * @return
     */
    @Override
    public AppUserCardVo queryByCardNo(String cardNo) {
        AppUserCardBo bo = new AppUserCardBo();
        bo.setCardNo(cardNo);
        LambdaQueryWrapper<AppUserCard> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoOne(lqw);
    }
}
